in vec3 Normal;

flat in vec3 color;
flat in vec3 back_color;

uniform bool do_texture_mapping;

uniform int sourcex;
uniform int sourcey;
uniform int destinationx;
uniform int destinationy;


struct LightInfo
{
	vec4 position;
	vec3 la;
	vec3 ld;
	vec3 ls;
};

struct MaterialInfo
{
	vec3 ka;
	vec3 kd;
	vec3 ks;
	float shininess;
};

uniform LightInfo light;
uniform MaterialInfo material;
uniform sampler2D backgroundTex;

layout (location = 0) out vec4 FragColor;
in vec2 tcoord;

vec3 ads( )
{
  vec3 n = normalize( Normal );

  if (!gl_FrontFacing)
	n = -n;

  vec3 s = normalize( vec3(light.position) - Position );
  vec3 v = normalize(vec3(-Position));
  vec3 r = reflect(-s, n);
  float s_dot_n = max(dot(s, n), 0.0);

  return light.la * material.ka + light.ld * material.kd * s_dot_n + (s_dot_n > 0 ? light.ls * material.ks * pow(max(dot(r,v), 0.0), material.shininess) : vec3(0.0));
}

void main()
{
	float distance = sqrt((destinationx - sourcex)*(destinationx - sourcex) + (destinationy - sourcey)*(destinationy - sourcey));
	float distance2 = sqrt((gl_FragCoord.x - sourcex) * (gl_FragCoord.x - sourcex) + (gl_FragCoord.y - sourcey) * (gl_FragCoord.y - sourcey));
	
	
	if (do_texture_mapping)
	{
		if (distance2 <= distance)
		{
			FragColor = texture2D(backgroundTex, tcoord) * vec4(.3, .6, .7, 1.0);
		}
		else
		{
			FragColor = texture2D(backgroundTex, tcoord);
		}
		//FragColor = FragColor * (texture2D(backgroundTex, tcoord));
		//FragColor = texture2D(backgroundTex, tcoord);
	}
	else
		FragColor = vec4(ads(), 1.0);
}
